package com.tobestronger.gulimall.product.service.impl;

import com.tobestronger.gulimall.product.model.CategoryEntityWithChildren;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tobestronger.common.utils.PageUtils;
import com.tobestronger.common.utils.Query;

import com.tobestronger.gulimall.product.dao.CategoryDao;
import com.tobestronger.gulimall.product.entity.CategoryEntity;
import com.tobestronger.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntityWithChildren> listWithTree() {
        List<CategoryEntity> allCategoryEntities = categoryDao.selectList(null);

        //转为CategoryEntityWithChildren
        List<CategoryEntityWithChildren> categoryEntityWithChildrenList = allCategoryEntities
                .stream()
                .map(categoryEntity -> {
                    CategoryEntityWithChildren categoryEntityWithChildren = new CategoryEntityWithChildren();
                    categoryEntityWithChildren.setChildren(new ArrayList<>());
                    categoryEntityWithChildren.setCatId(categoryEntity.getCatId());
                    categoryEntityWithChildren.setName(categoryEntity.getName());
                    categoryEntityWithChildren.setParentCid(categoryEntity.getParentCid());
                    categoryEntityWithChildren.setCatLevel(categoryEntity.getCatLevel());
                    categoryEntityWithChildren.setShowStatus(categoryEntity.getShowStatus());
                    categoryEntityWithChildren.setSort(categoryEntity.getSort());
                    categoryEntityWithChildren.setIcon(categoryEntity.getIcon());
                    categoryEntityWithChildren.setProductUnit(categoryEntity.getProductUnit());
                    categoryEntityWithChildren.setProductCount(categoryEntity.getProductCount());

                    return categoryEntityWithChildren;
                })
                .collect(Collectors.toList());

        //转为map结构
        Map<Long,CategoryEntityWithChildren> entitiesMap = new HashMap<>();
        categoryEntityWithChildrenList.forEach(categoryEntityWithChildren -> {
            entitiesMap.put(categoryEntityWithChildren.getCatId(),categoryEntityWithChildren);
        });

        //放到parent的children中
        entitiesMap.forEach((catId,entity)->{
            CategoryEntityWithChildren parent = entitiesMap.get(entity.getParentCid());
            if (null != parent){
                parent.getChildren().add(entity);
            }
        });

        //排序children
        entitiesMap.forEach((catId,entity)->{
            List<CategoryEntityWithChildren> sortedChildren = entity.getChildren()
                    .stream()
                    .sorted((a, b) -> {
                        Integer aSort = a.getSort() == null ? 0 : a.getSort();
                        Integer bSort = b.getSort() == null ? 0 : b.getSort();
                        return aSort - bSort;
                    })
                    .collect(Collectors.toList());
            entity.setChildren(sortedChildren);
        });

        //获取所有一级分类并排序返回
        List<CategoryEntityWithChildren> allRootCategoryEntityWithChildrenList = entitiesMap.values()
                .stream()
                .filter(categoryWithChildren -> {
                    //一级分类的parentCid都是0
                    return categoryWithChildren.getParentCid() == 0;
                })
                .sorted((a, b) -> {
                    Integer aSort = a.getSort() == null ? 0 : a.getSort();
                    Integer bSort = b.getSort() == null ? 0 : b.getSort();
                    return aSort - bSort;
                })
                .collect(Collectors.toList());

        return allRootCategoryEntityWithChildrenList;
    }

    @Override
    public void deleteMenusByCatIds(Long[] catIds) {
        categoryDao.deleteBatchIds(Arrays.asList(catIds));
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        ArrayList<Long> catelogPathList = new ArrayList<>();

        while (catelogId!=0){
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            catelogPathList.add(categoryEntity.getCatId());
            catelogId = categoryEntity.getParentCid();
        }

        return catelogPathList.toArray(new Long[1]);
    }

}